﻿using System;
using System.Windows.Forms;
using ControleRepresentanteSync.Application;
using ControleRepresentanteSync.Application.Pedido;
using ControleRepresentanteSync.Application.Setup;
using ControleRepresentanteSync.Application.Wizard;
using ControleRepresentanteSync.View.Pedido;
using ControleRepresentanteSync.View.Setup;
using ControleRepresentanteSync.View.Wizard;

namespace ControleRepresentanteSync.View
{
    public partial class MainWindow : IMainWindowViewModel
    {
        private readonly NotifyIcon m_NotifyIcon;

        public MainWindow()
        {
            InitializeComponent();

            ShowInTaskbar = false;
            Hide();

            m_NotifyIcon = new NotifyIcon
            {
                Icon = Properties.Resources.Idle,
                Text = "Controle Representante Sync",
            };

            MenuItem sairMenuItem = new MenuItem("Sair");
            sairMenuItem.Click += SairMenuItemClick;
            m_NotifyIcon.ContextMenu = new ContextMenu(new[]
            {
                new MenuItem("-"),
                sairMenuItem
            });

            ViewModelFactory viewModelFactory = new ViewModelFactory();
            RegisterViewModels(viewModelFactory);

            IUserInterfaceService userInterfaceService = new UserInterfaceService(Dispatcher);

            new MainWindowPresenter(this, viewModelFactory, userInterfaceService);

            m_NotifyIcon.Visible = true;
        }

        private static void RegisterViewModels(ViewModelFactory viewModelFactory)
        {
            viewModelFactory.Register<IWizardViewModel, WizardViewModel>();
            viewModelFactory.Register<IPedidoListViewModel, PedidoListViewModel>();
            viewModelFactory.Register<IWelcomePageViewModel, WelcomePageViewModel>();
            viewModelFactory.Register<IUserPageViewModel, UserPageViewModel>();
            viewModelFactory.Register<IStartupPageViewModel, StartupPageViewModel>();
            viewModelFactory.Register<ISectionItem, SectionItem>();
            viewModelFactory.Register<IPedidoViewModel, PedidoViewModel>();
            viewModelFactory.Register<IFabricaItem, FabricaItem>();
            viewModelFactory.Register<ITransportadoraItem, TransportadoraItem>();
            viewModelFactory.Register<IVendedorItem, VendedorItem>();
            viewModelFactory.Register<IClienteItem, ClienteItem>();
            viewModelFactory.Register<IProdutoItem, ProdutoItem>();
            viewModelFactory.Register<IPedidoSyncViewModel, PedidoSyncViewModel>();
            viewModelFactory.Register<IDividerSectionItem, DividerSectionItem>();
            viewModelFactory.Register<ISimpleFinishViewModel, SimpleFinishViewModel>();
            viewModelFactory.Register<IAttributeItem, AttributeItem>();
            viewModelFactory.Register<IAttributeListViewModel, AttributeListViewModel>();
        }

        private void SairMenuItemClick(object sender, EventArgs e)
        {
            m_NotifyIcon.Visible = false;
            m_NotifyIcon.Dispose();
            FireOnClose();
        }

        public IWindow CreateWizardWindow(IWizardViewModel wizardViewModel)
        {
            return new WizardWindow { DataContext = wizardViewModel };
        }

        public void AddSection(ISectionItem sectionItem)
        {
            MenuItem menuItem = new MenuItem(sectionItem.Title){Checked = sectionItem.IsChecked == true};
            menuItem.Click += (s, e) =>
            {
                m_NotifyIcon.Visible = false;
                try
                {
                    sectionItem.FireOpen();
                }
                finally
                {
                    m_NotifyIcon.Visible = true;
                }
            };
            sectionItem.IsCheckedChanged += (s, e) =>
            {
                menuItem.Checked = sectionItem.IsChecked == true;
            };
            sectionItem.TitleChanged += (s, e) =>
            {
                menuItem.Text = sectionItem.Title;
            };
            m_NotifyIcon.ContextMenu.MenuItems.Add(0, menuItem);
        }

        public event EventHandler OnClose;

        public void FireOnClose()
        {
            if (OnClose != null)
                OnClose(this, EventArgs.Empty);
        }
    }
}
